package com.atguigu.gulimall.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.List;

@Controller
public class HelloController {

    @Value("${sso.server.url}")
    private String loginUrl;

    @ResponseBody
    @GetMapping("/hello")
    public String hello() {
        return "hello";
    }


    @GetMapping("/emp")
    public String getEmps(Model model, HttpSession session, @RequestParam(value = "token",required = false)String token) {
        if (!StringUtils.isEmpty(token)) {
            //認爲已經登錄,將用戶放在session中
            session.setAttribute("loginUser", "zs");
        }
        Object loginUser = session.getAttribute("loginUser");
        if (loginUser == null) {
          return "redirect:" + loginUrl + "?url=http://client1.com:8081/emp";
        } else {
            List<String> emps = Arrays.asList("張三", "李斯");
            model.addAttribute("emps", emps);
            return "list";
        }
    }
}
